﻿using Engine;
using Engine.Graphics;
using Game;
namespace Mekiasm
{
    public class CircleImageWidget : Widget
    {
        public float ImageWidth { set; get; }
        public float ImageHeight { set; get; }
        public Vector2 TexCoord1 = new Vector2(0.5f, 0.5f);
        public Vector2 WidgetCoord1;
        public TexturedBatch2D texturedBatch2D;
        public TexturedBatch2D m_texturedBatch2D;
        public float S = 0f;//弧度
        public float x = 0f;
        public float y = 0f;
        public int count = 0;
        public Color full = Color.White;
        public Color bank = new Color(125, 125, 125);
        public Texture2D m_texture;
        public Texture2D m_texture2D;
        public bool m_isFlip;
        public bool isFlip
        {
            get { return m_isFlip; }
            set { if (value) { bank = Color.White; full = new Color(125, 125, 125); } else { bank = new Color(125, 125, 125); full = Color.White; } m_isFlip = value; }
        }
        public float m_progress;
        public float Progess
        {
            get { return m_progress; }
            set { if (!isFlip) m_progress = value; else m_progress = 1f - value; }
        }
        public Texture2D Texture
        {
            get
            {
                return m_texture;
            }
            set
            {
                m_texture = value;
                m_texture2D = value;
                ImageHeight = value.Height;
                ImageWidth = value.Width;
                WidgetCoord1 = new Vector2(ImageWidth / 2f, ImageHeight / 2f);
            }
        }
        public override void MeasureOverride(Vector2 parentAvailableSize)
        {
            base.MeasureOverride(parentAvailableSize);
            IsDrawRequired = Progess > 0f;
        }
        public override void Draw(DrawContext dc)
        {
            if (Texture != null)
            {
                texturedBatch2D = dc.PrimitivesRenderer2D.TexturedBatch(m_texture, useAlphaTest: false, 1, DepthStencilState.None, null, BlendState.NonPremultiplied, SamplerState.PointWrap);
                count = texturedBatch2D.TriangleVertices.Count;
                m_texturedBatch2D = dc.PrimitivesRenderer2D.TexturedBatch(m_texture2D, useAlphaTest: false, 0, DepthStencilState.None, null, BlendState.NonPremultiplied, SamplerState.PointWrap);
                if (m_progress >= 0f)
                {//0.5-1 x                    
                    if (m_progress > 0.125f) S = 0.125f * MathUtils.PI * 4; else S = m_progress * MathUtils.PI * 4;
                    x = 0.5f + 0.5f * MathUtils.Sin(S); y = 0;
                    texturedBatch2D.QueueTriangle(WidgetCoord1, new Vector2(WidgetCoord1.X, 0f), new Vector2(ImageWidth * x, ImageHeight * y), 1f, TexCoord1, new Vector2(TexCoord1.X, 0f), new Vector2(x, y), full);
                }
                if (m_progress > 0.125f)
                {//0-0.5 y
                    if (m_progress > 0.25f) S = 0.125f * MathUtils.PI * 4; else S = (m_progress - 0.125f) * MathUtils.PI * 4;
                    x = 1f; y = 0.5f * MathUtils.Sin(S);
                    texturedBatch2D.QueueTriangle(WidgetCoord1, new Vector2(ImageWidth, 0f), new Vector2(ImageWidth * x, ImageHeight * y), 1f, TexCoord1, new Vector2(1f, 0f), new Vector2(x, y), full);
                }
                if (m_progress > 0.25f)
                {//0.5-1 y
                    if (m_progress > 0.375f) S = 0.125f * MathUtils.PI * 4; else S = (m_progress - 0.25f) * MathUtils.PI * 4;
                    x = 1f; y = 0.5f + 0.5f * MathUtils.Sin(S);
                    texturedBatch2D.QueueTriangle(WidgetCoord1, new Vector2(ImageWidth, WidgetCoord1.Y), new Vector2(ImageWidth * x, ImageHeight * y), 1f, TexCoord1, new Vector2(1f, 0.5f), new Vector2(x, y), full);
                }
                if (m_progress > 0.375f)
                {//1-0.5 x
                    if (m_progress > 0.5f) S = 0.125f * MathUtils.PI * 4; else S = (m_progress - 0.375f) * MathUtils.PI * 4;
                    x = 1f - 0.5f * MathUtils.Sin(S); y = 1f;
                    texturedBatch2D.QueueTriangle(WidgetCoord1, new Vector2(ImageWidth, ImageHeight), new Vector2(ImageWidth * x, ImageHeight * y), 1f, TexCoord1, Vector2.One, new Vector2(x, y), full);
                }
                if (m_progress > 0.5f)
                {//0.5-0 x
                    if (m_progress > 0.625f) S = 0.125f * MathUtils.PI * 4; else S = (m_progress - 0.5f) * MathUtils.PI * 4;
                    x = 0.5f - 0.5f * MathUtils.Sin(S); y = 1f;
                    texturedBatch2D.QueueTriangle(WidgetCoord1, new Vector2(WidgetCoord1.X, ImageHeight), new Vector2(ImageWidth * x, ImageHeight * y), 1f, TexCoord1, new Vector2(0.5f, 1f), new Vector2(x, y), full);
                }
                if (m_progress > 0.625f)
                {//1-0.5 y
                    if (m_progress > 0.75f) S = 0.125f * MathUtils.PI * 4; else S = (m_progress - 0.625f) * MathUtils.PI * 4;
                    x = 0f; y = 1f - 0.5f * MathUtils.Sin(S);
                    texturedBatch2D.QueueTriangle(WidgetCoord1, new Vector2(0f, ImageHeight), new Vector2(ImageWidth * x, ImageHeight * y), 1f, TexCoord1, Vector2.UnitY, new Vector2(x, y), full);
                }
                if (m_progress > 0.75f)
                {//0.5-0 y
                    if (m_progress > 0.875f) S = 0.125f * MathUtils.PI * 4; else S = (m_progress - 0.75f) * MathUtils.PI * 4;
                    x = 0f; y = 0.5f - 0.5f * MathUtils.Sin(S);
                    texturedBatch2D.QueueTriangle(WidgetCoord1, new Vector2(0f, WidgetCoord1.Y), new Vector2(ImageWidth * x, ImageHeight * y), 1f, TexCoord1, new Vector2(0f, 0.5f), new Vector2(x, y), full);
                }
                if (m_progress > 0.875f)
                {//0-0.5 x
                    S = (m_progress - 0.875f) * 4 * MathUtils.PI;
                    x = 0.5f * MathUtils.Sin(S); y = 0;
                    texturedBatch2D.QueueTriangle(WidgetCoord1, Vector2.Zero, new Vector2(ImageWidth * x, ImageHeight * y), 1f, TexCoord1, Vector2.Zero, new Vector2(x, y), full);
                }
                texturedBatch2D.TransformTriangles(GlobalTransform, count);
                m_texturedBatch2D.QueueQuad(Vector2.Zero, WidgetCoord1 * 2, 1f, Vector2.Zero, Vector2.One, bank);
                m_texturedBatch2D.TransformTriangles(GlobalTransform, count);
            }

        }
    }


}
